<template>
  <section>
    <!-- 面包屑 -->
 <breadcrumbs></breadcrumbs>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="字典名称"
                    prop="name">
        <el-input v-model="searchForm.name"
                  placeholder="请输入字典名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"></btns>
    <div class="data-container scroll-table">
      <div>
        <div class="ind-title lh50 f16">字典列表</div>
      </div>
      <el-table :data="tableData"
                style="width: 100%"
                row-key="dictId"
                border
                lazy
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="40">
        </el-table-column>
        <el-table-column prop="dictName"
                         label="名称">
        </el-table-column>
        <el-table-column prop="dictCode"
                         label="code">
        </el-table-column>
        <el-table-column prop="dictValue"
                         label="值">
        </el-table-column>
        <el-table-column prop="dictDesc"
                         label="描述">
        </el-table-column>
        <el-table-column prop="dictSort"
                         label="排序">
        </el-table-column>
        <el-table-column fixed="right"
                         label="操作"
                         width="100">
          <template slot-scope="scope">
            <div :underline="false"
                     type="primary"
                     v-if="scope.row.dictPid==0"
                     @click="addnext(scope.row)">新增下级</div>&nbsp;
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--新增修改界面-->
    <el-dialog :title="(funTit=='add'?'新增':'修改')+(pForm?'下级-'+pForm.dictName:'')"
               :visible.sync="addORUpdateVisible"
               width="700px"
               :close-on-click-modal="false">
      <el-form :model="dictForm"
               label-width="80px"
               :rules="rules"
               ref="dictForm">
        <el-form-item label="字典名称"
                      prop="dictName">
          <el-input v-model="dictForm.dictName"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="Code"
                      prop="">
          <el-input v-model="dictForm.dictCode"
                    auto-complete="off"
                    :disabled="ischild"></el-input>
        </el-form-item>
        <el-form-item label="值"
                      prop="dictValue">
          <el-input v-model="dictForm.dictValue"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序"
                      prop="dictSort">
          <el-input v-model="dictForm.dictSort"
                    auto-complete="off"
                    @input="dictForm.dictSort=dictForm.dictSort.replace(/[^\d^\.]+/g,'')"
                    type="text"></el-input>
        </el-form-item>
        <el-form-item label="描述"
                      prop="dictDesc">
          <el-input v-model="dictForm.dictDesc"
                    auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click.native="addORUpdateVisible = false">取消</el-button>
        <el-button type="primary"
                   @click.native="submit('dictForm')">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import btns from '@/components/btns.vue';
import breadcrumbs from "@/components/breadcrumbs.vue";

export default {
  components: {
    btns,
    breadcrumbs,
  },
  data () {
    return {
      searchForm: {
        name: ''
      },
      ischild: false,
      data: [],
      tableData: [],
      tableSelection: [],
      addORUpdateVisible: false,
      funTit: '',
      pForm: null,
      dictForm: {
        "dictCode": "",
        "dictDesc": "",
        "dictName": "",
        "dictPid": 0,
        "dictSort": 0,
        "dictValue": ""
      },
      rules: {
        "dictCode": [
          { required: true, message: '请输入字典Code', trigger: 'blur' }
        ],
        "dictName": [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        "dictValue": [
          { required: true, message: '请输入字典值', trigger: 'blur' }
        ],
        "dictDesc": [],
        "dictSort": [
          { required: true, message: '排序', trigger: 'blur' }
        ]
      },
    }
  },
  created () {
    this.dictlist();
    // console.log('tableData', this.tableData);
  },
  methods: {
    handleReset () {
      this.$refs.searchForm.resetFields();
      this.search()
    },
    search () {
      if (this.searchForm.name) {
        var data = this.data.filter(e => e.dictName.indexOf(this.searchForm.name) >= 0 && e.pid != 0 || e.pid == 0);
        this.getdata(data, true);
      } else {
        var data = this.data
        this.getdata(data, true);
      }
    },
    dictlist () {
      //获取接口
      this.$api.dict.list({}).then(d => {
        if (d.code == 200) {
          this.data = d.data;
          this.getdata(this.data);
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    getdata (nowdata) {
      var data = nowdata.filter(e => e.dictPid == 0);
      var temp = [];
      data.forEach(e => {
        e.children = nowdata.filter(c => c.dictPid == e.dictId);
        temp.push(e)
      })
      // console.log(temp);
      this.tableData = temp;
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {
      this.pForm = null;
      this.addORUpdateVisible = true;
      this.dictForm = {
        "dictCode": "",
        "dictDesc": "",
        "dictName": "",
        "dictPid": 0,
        "dictSort": 0,
        "dictValue": ""
      }
    },
    addnext (p) {
      this.funTit = 'add';
      this.pForm = p;
      this.addORUpdateVisible = true;
      this.menuForm = {
        "dictCode": "",
        "dictDesc": "",
        "dictName": "",
        "dictPid": p.dictId,
        "dictSort": 0,
        "dictValue": ""
      }
      this.dictForm = this.menuForm;
      this.ischild = true;
      console.log(this.menuForm)
    },
    update () {
      this.pForm = null
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      this.dictForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    del () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }
      var demo = this.tableSelection[0]
      if (demo.children && demo.children.length > 0) {
        this.$message.error('还有子级字典不能删除');
        return
      }
      this.$confirm('此操作将删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.dict.del({ dictId: demo.dictId }).then(d => {
          if (d.code == 200) {
            this.$message.success('删除成功');
            this.dictlist();
          } else {
            this.$message.error(d.msg);
          }
        }).catch((e) => {
          console.log(e);
        })
      });
    },
    refresh () {
      this.dictlist();
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.funTit == 'add') {
            console.log('form', this.dictForm);
            this.$api.dict.add(this.dictForm).then(d => {
              if (d.code == 200) {
                this.$message.success('新增成功');
                this.addORUpdateVisible = false;
                this.$refs.dictForm.resetFields();
                this.dictlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.dictForm.children = null;
            this.$api.dict.update(this.dictForm).then(d => {
              if (d.code == 200) {
                this.$message.success('修改成功');
                this.addORUpdateVisible = false;
                this.$refs.dictForm.resetFields();
                this.dictlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>

<style>
.mytransfer .el-checkbox {
  display: block;
}
</style>
